﻿using System.Collections.Generic;
using ESRI.ArcGIS.Carto;
using Inovout.Gis;

namespace Inovout.ArcGis
{
    public class MapInfo : IMapInfo
    {
        private IMap arcMap;

        internal IMap ArcMap
        {
            get { return arcMap; }
        }
        public MapInfo(IMap arcMap)
        {
            this.arcMap = arcMap;
        }

        private List<Gis.ILayerInfo> layers;
        public IEnumerable<Gis.ILayerInfo> Layers
        {
            get
            {
                if (layers == null)
                {
                    layers = new List<Gis.ILayerInfo>();
                    for (int i = 0; i < arcMap.LayerCount; i++)
                    {
                        layers.Add(ArcGisHelper.CreateLayer(null, arcMap.get_Layer(i)));
                    }
                }
                return layers;
            }
        }

        private List<Gis.ILayerInfo> allLayers;
        public IEnumerable<Gis.ILayerInfo> AllLayers
        {
            get
            {
                if (allLayers == null)
                {
                    allLayers = new List<Gis.ILayerInfo>();
                    foreach (var layer in Layers)
                    {
                        allLayers.Add(layer);
                        if (layer is IGroupLayerInfo)
                        {
                            FillAllLayers(layer as IGroupLayerInfo);
                        }

                    }

                }
                return allLayers;
            }
        }

        private void FillAllLayers(IGroupLayerInfo groupLayer)
        {
            foreach (var layer in groupLayer.Layers)
            {
                allLayers.Add(layer);
                if (layer is IGroupLayerInfo)
                {
                    FillAllLayers(layer as IGroupLayerInfo);
                }

            }
        }

    }
}